package com.tqw.item.service.impl;

import com.tqw.entity.JWTUtil;
import com.tqw.item.mapper.FavoriteMapper;
import com.tqw.item.mapper.ItemMapper;
import com.tqw.item.service.FavoritesService;
import com.tqw.pojo.Client;
import com.tqw.pojo.Favorites;
import com.tqw.pojo.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @description: some desc
 * @author: Administrator
 * @date: 2022/4/12 15:12
 */
@Service
public class FavoritesServiceImpl implements FavoritesService {
    @Autowired
    private FavoriteMapper favoriteMapper;
    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public String findfavorites(String token, Integer itemId) {
        try {
            Client client = JWTUtil.getClient(token);
            Integer userId = client.getId();
            Favorites favorites = favoriteMapper.findfavorites(userId, itemId);
            if (favorites == null) {
                return "加入收藏夹";
            } else {
                return "取消收藏";
            }
        } catch (Exception e) {
            return "加入收藏夹";
        }
    }

    @Override
    public Map<String, String> updateFavorite(String token, Integer itemId) {
        Client client = JWTUtil.getClient(token);
        Integer userId = client.getId();
        Item item = itemMapper.selectById(itemId);
        if ((int) userId==(int) item.getUserId()){
            return null;
        }
        Favorites favorites = favoriteMapper.findfavorites(userId, itemId);
        Map<String, String> map = new HashMap<>();
        if (favorites == null) {
            insertfavorites(userId, itemId);
            map.put("favoritese", "取消收藏");
            map.put("hint", "添加收藏成功");
            return map;
        } else {
            deletefavorites(favorites.getId());
            map.put("favoritese", "添加收藏夹");
            map.put("hint", "取消收藏成功");
            return map;
        }
    }

    @Override
    public Integer findToken(String token, Integer itemId) {
        Map<String, Object> map = new HashMap<>();
        ValueOperations<String, Map<String, Object>> vo = redisTemplate.opsForValue();
        Map<String, Object> itemMap = vo.get("itemId:" + itemId);
        Character a = '1';
        if (itemMap == null) {
            System.out.println("走数据库");
            //商品信息
            Item item = itemMapper.selectById(itemId);
            if (!a.equals(item.getStatus())) {
                return null;
            }
        } else {
            System.out.println("走缓存");
            Item item= (Item) itemMap.get("item");
            //商品信息
            if (!a.equals(item.getStatus())) {
                return null;
            }
        }
        return 200;
    }

    public void insertfavorites(Integer userId, Integer itemId) {
        Favorites favorites = new Favorites().setUserId(userId).setItemId(itemId);
        favoriteMapper.insert(favorites);
    }

    public void deletefavorites(Integer id) {
        favoriteMapper.deleteById(id);
    }


}
